﻿#include<iostream>
using namespace std;
#define m 4
#define n 4
typedef struct
{
	int val;//值
	int i, j;//行列下标
}Trimat;
void print(int B[4][4])
{
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			cout << B[i][j] ;
		}
		cout << endl;//每输出矩阵一行元素后就换行

	}
}
//建立三元组
void create(int A[4][4], int B[][3])//m是行，n是列
{//i行，j列
	
	int k = 0;
	for (int i = 0; i < 4; ++i)
	{
		for (int j = 0; j < 4; ++j)
		{
			if (A[i][j] != 0)
			{
				B[k][0] = i;
				B[k][1] = j;
				B[k][2] = A[i][j];
				k++;
			}
		}
	}
	cout << "原矩阵的三元组为：" << endl;
	for (int s = 0; s < k; s++)
	{
		cout << B[s][0] << " " << B[s][1] << " " << B[s][2] << " " << endl;;
	}
}
//转置
void t(int A[4][4], int B[4][4])
{
	for (int i = 0; i < m; ++i)
		for (int j = 0; j < n; ++j)
			B[j][i] = A[i][j];
	cout << "转置后的矩阵为：" << endl;
	print(B);
}
int main()
{
	int A[4][4] = { {0,4,0,0},{2,0,0,6},{0,5,0,0},{2,0,0,0}};
	int B[][3] = { 0 };
	int D[4][4] = { 0 };
	cout << "输入的稀疏矩阵为：" << endl;
	print(A);
	t(A,D);
	create(A,B);
	return 0;
}